package me.zk.repo;

import me.zk.bo.SysUserBo;
import me.zk.entity.SysUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: zhang kai
 * @Date: 2022/11/18
 * @Description:
 */
@Repository
public interface SysUserJpaRepo extends JpaRepository<SysUser,Long>, JpaSpecificationExecutor<SysUser> {

    SysUser findByUserName(String userName);
    SysUser findByUserNameAndEmail(String userName, String email);
    SysUser findByUserNameLike(String userName);

    Page<SysUser> findByUserNameLike(String userName, Pageable pageable);

    SysUser findByUserName(String userName, Sort sort);

    @Query(value = "select u from SysUser u where  u.userName like :username")
    Page<SysUser> findUser(@Param("username") String userName, Pageable pageable);

    //需要全部参数的构造器
    @Query(value = "select new me.zk.bo.SysUserBo(u.userId,u.userName,u.password,u.email) " +
            "from SysUser u where u.userName= :username")
    Page<SysUserBo> findByName(@Param("username") String userName, Pageable pageable);



    @Query(value = "select * from sys_user where user_name= :username", nativeQuery = true)
    Page<SysUser> findUserByName(@Param("username") String userName, Pageable pageable);


    @Query(value = "select user_id,user_name,email from sys_user where user_name= :username", nativeQuery = true)
    List<Map<String,Object>> queryUserByName(@Param("username") String userName);


    @Query(nativeQuery = true, value = "delete from sys_user where user_name = :username ")
    @Modifying
    @Transactional
    int deleteByName(@Param("username") String userName);


    @Query(value = "delete from SysUser s where s.userId = ?1 ")
    @Modifying
    @Transactional
    int deleteByUserId(Long userId);
}
